/**
 * 商品经营范围维护 - 编辑页面
 * @author shixc
 * @date 2018/01/31
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            form: {
                GWDA007_NAME: null,
                GWAML003: null,
                GWAML004: null
            },
            rules: {
                GWDA002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDA003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDA006: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDA007_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]

            },
            selectOptions: {
                sys_y_n: app.datas_("sys_y_n"),
                sys: app.datas_("crm_gwai501_gwax011"),
                crm_gwdc001_gwda006: app.datas_("crm_gwdc001_gwda006")
            }
        },
        callObj
    ;

    var Gwdc001Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwdc001_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }
                ]
            }, win);
        },
        edit: function () {
            app.ajax({
                xurl: 'crm/gwdc001/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '商品经营范围信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwdc001').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },
        add: function () {
            app.ajax({
                xurl: 'crm/gwdc001/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '商品经营范围信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwdc001').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },
        _call: function (obj) {
            callObj = obj;
            if (obj.is_add) {
                vueData.isAdd = true;
                var myDate = new Date();
                var year = myDate.getFullYear();
                var month = myDate.getMonth() + 1;
                var strDate = myDate.getDate();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                var time = year + '-' + month + '-' + strDate;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        GWDA004: time,
                        GWDA005: 'Y',
                        GWDAACTI: 'Y'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['GWDA'], vueData.form);
            var vue = new Vue({
                el: '#gwdc001_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        self._closeTpl();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.add();
                                } else {
                                    self.edit();
                                }
                            }
                        });
                    },
                    openGwak: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwak001',
                                    columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                    ],
                                    //url: 'admin/getGwakData'
                                },
                                cb: function (data) {
                                    vueData.form.GWDA002 = data.GWAK001;
                                    vueData.form.GWAKL004 = data.GWAKL003;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择客户',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwam: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwam001',
                                    searchParam: {
                                        GWAM004: vueData.form.GWDA007,
                                        GWAM030: '0'
                                    },
                                    columnList: [
                                        {label: '产品编号', prop: 'GWAM001', width: '150'},
                                        {label: '产品名称', prop: 'GWAML003', width: '150'},
                                        {label: '规格', prop: 'GWAML004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWDA003 = data.GWAM001;
                                    vueData.form.GWAML003 = data.GWAML003;
                                    vueData.form.GWAML004 = data.GWAML004;
                                    vueData.form.GWDA007 = data.GWAM004;
                                    vueData.form.GWDA007_NAME = data.GWAOL003;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择产品',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    openGwao: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwao001',
                                    searchParam: {
                                        GWAO002: vueData.form.GWAM007
                                    },
                                    columnList: [
                                        { label: '产品系列编号', prop: 'GWAO001', width: '150'},
                                        { label: '说明', prop: 'GWAOL003', width: '150'},
                                        { label: '助记码', prop: 'GWAOL004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWDA007 = data.GWAO001;
                                    vueData.form.GWDA007_NAME = data.GWAOL003;
                                    vueData.form.GWDA003 = '';
                                    vueData.form.GWAML003 = '';
                                    vueData.form.GWAML004 = '';
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择产品系列',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    changeGwda003: function () {
                        if (vueData.form.GWDA003 != '' && vueData.form.GWDA003 != null) {
                            if (vueData.form.GWDA003 != '*') {
                                app.ajax({
                                    xurl: 'crm/gwdc001/checkGwda003',
                                    xdata: {GWDA003: vueData.form.GWDA003, GWAM004: vueData.form.GWDA007},
                                    cb: function (result) {
                                        if (result.ok) {
                                            if (result.data.data != null && result.data.data != "") {
                                                vueData.form.GWAML003 = result.data.data.GWAML003;
                                                vueData.form.GWAML004 = result.data.data.GWAML004;
                                                vueData.form.GWDA007 = result.data.data.GWAM004;
                                                vueData.form.GWDA007_NAME = result.data.data.GWAOL003;
                                            } else {
                                                vueData.form.GWDA003 = '';
                                                var message = '';
                                                if(vueData.form.GWDA007 != null && vueData.form.GWDA007 != ''){
                                                    message = '该产品编号不存在于系列' + vueData.form.GWDA007 + '中!';
                                                }else{
                                                    message = "该产品编号不存在!";
                                                }
                                                app.showMessage(message);
                                                return;
                                            }
                                        }
                                    }
                                });
                            }
                        }else {
                            vueData.form.GWAML003 = '';
                            vueData.form.GWAML004 = '';
                        }
                    },
                    gwda007: function () {
                        if (vueData.isAdd) {
                            if (vueData.form.GWDA007_NAME != '' && vueData.form.GWDA007_NAME != null) {
                                if(vueData.form.GWDA003 != '' && vueData.form.GWDA003 != null) {
                                    app.ajax({
                                        xurl: 'crm/gwdc001/checkGwda007',
                                        xdata: {GWDA007: vueData.form.GWDA007},
                                        cb: function (result) {
                                            if (result.ok) {
                                                if (result.data.data != null && result.data.data != "") {
                                                    vueData.form.GWDA007_NAME = result.data.data.GWAOL003;
                                                    vueData.form.GWDA007 = result.data.data.GWAO001;
                                                } else {
                                                    vueData.form.GWDA007_NAME = '';
                                                    vueData.form.GWDA007 = '';
                                                    app.showMessage("该产品系列不存在!");
                                                    return;
                                                }
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    },
                    clearGwda002:function () {
                        if(vueData.form.GWDA002 != '' || vueData.form.GWDA002 != null){
                            vueData.form.GWAKL004 = '';
                        }
                    },
                    clearGwda007:function () {
                        if(vueData.form.GWDA007_NAME != '' || vueData.form.GWDA007_NAME != null){
                            vueData.form.GWDA003 = '';
                            vueData.form.GWAML003 = '';
                            vueData.form.GWAML003 = '';
                        }
                    },
                    clearGwda003:function () {
                        if(vueData.form.GWDA003 != '' || vueData.form.GWDA003 != null){
                            vueData.form.GWAML003 = '';
                            vueData.form.GWAML003 = '';
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwdc001_edit');
                },
                watch: {
                    'form.GWDA007_NAME': function () {
                        if (vueData.form.GWDA007_NAME == '') {
                            vueData.form.GWDA007 = '';
                        }
                    },
                    'form.GWDA003': function () {
                        if (vueData.form.GWDA003 == '') {
                            vueData.form.GWAML003 = '';
                            vueData.form.GWAML004 = '';
                        }
                    }
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwdc001Edit();

});